<template>
  <el-dialog :visible.sync="visible" title="导入" width="30%" append-to-body :close-on-click-modal="false">
    <FileUpload v-model="file" @input="getFile"/>
    <div style="margin-top: 20px">
      <p>请将 <el-link type="primary" @click="downloadTemplate"> 模板文件</el-link> 下载到本地，并将数据填写完成后再上传到系统。</p>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="SubmitEvent">导入</el-button>
      <el-button @click="closeDialog">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FileUpload from "@/components/upload/FileUpload.vue";
//import { importData } from "@/api/customer.js";
export default {
  components: {
    FileUpload,
  },
  data() {
    return {
      visible: false,
      file: null,
    };
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    SubmitEvent() {
      if (!this.file) {
        this.$message({
          type: "error",
          message: "请选择文件!",
        });
        return;
      }
			console.log(this.file[0].response.data.url);
			//return;
			this.$api({
				url: '/admin_service.php',
				method: 'get',
				data: {
					action: "users_commissionImport",
					filepath: this.file[0].response.data.url,
				}
			}).then(res => {
				if (res.code == 200) {
					this.$parent.setView()
					 this.closeDialog();
				} else {
					alertErr(res.msg)
				}
			})
			
      /* importData({
        file: this.file[0].response.url,
      }).then((res) => {
        this.$message({
          type: "success",
          message: "导入成功!",
        });
        this.closeDialog();
        this.$parent.fetchData();
      }); */
    },
    getFile(file) {
      //this.$parent.fetchData();
      //this.closeDialog();
    },
    downloadTemplate() {
      const link = document.createElement("a");
	  let host = location.origin;
	  if(process.env.NODE_ENV !== "production"){
		  host = 'https://web.krycomm.com/'
	  }
      link.href = `${location.origin}/用户佣金修改导入模板.xlsx`; // 这是模板文件的路径
      link.download = "用户佣金修改导入模板.xlsx"; // 下载后的文件名称
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
  },
};
</script>
